<template>
  <div>
    <el-dialog
      :title="obj.isAdd ? '角色添加' : '角色编辑'"
      :visible.sync="obj.isTrue"
      width="50%"
    >
      <el-form ref="form" :model="form" label-width="80px">
        <el-form-item label="角色名称">
          <el-input v-model="form.rolename"></el-input>
        </el-form-item>
        <el-form-item label="角色权限">
          <el-tree
            :data="menuList"
            show-checkbox
            node-key="id"
            ref="tree"
            :props="{ children: 'children', label: 'title' }"
          >
          </el-tree>
        </el-form-item>
        <el-form-item label="状态">
          <el-switch
            v-model="form.status"
            :active-value="1"
            :inactive-value="2"
          ></el-switch>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="cancel()">取 消</el-button>
        <el-button type="primary" @click="submit()" v-if="obj.isAdd"
          >添 加</el-button
        >
        <el-button type="primary" @click="edit()" v-else>编 辑</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { mapActions, mapGetters } from "vuex";
import { roleAdd, roleEdit, roleInfo } from "../../../request/api";
import { successAlert, warningAlert } from "../../../util/alert";
export default {
  props: ["obj"],
  data() {
    return {
      form: {
        rolename: "",
        menus: "",
        status: 1,
      },
    };
  },
  computed: {
    ...mapGetters({
      menuList: "menu/list",
    }),
  },
  methods: {
    ...mapActions({
      reqMenuList: "menu/reqChangeList",
      reqRoleList: "role/reqChangeList",
    }),
    //封装清空
    empty() {
      this.form = {
        rolename: "",
        menus: "",
        status: 1,
      };
      //清空树形控件
      this.$refs.tree.setCheckedKeys([]);
    },
    //点击取消
    cancel() {
      this.obj.isTrue = false;
      this.empty();
    },
    //点击添加
    submit() {
      this.form.menus = JSON.stringify(this.$refs.tree.getCheckedKeys());
      roleAdd(this.form).then((res) => {
        if (res.data.code == 200) {
          //成功的弹窗
          successAlert(res.data.msg);
          //关闭对话框并清空数据
          this.cancel();
          //重新请求列表数据
          this.reqRoleList();
        } else {
          //失败的弹窗
          warningAlert(res.data.msg);
        }
      });
    },
    //获取一条信息
    getOne(id) {
      roleInfo({ id: id }).then((res) => {
        //第一种方式
        // this.form = res.data.list
        // this.form.menus = JSON.parse(res.data.list.menus)

        //第二种方式
        this.form = {
          ...res.data.list,
          menus: JSON.parse(res.data.list.menus),
        };
        //设置树形控件的内容
        this.$refs.tree.setCheckedKeys(this.form.menus);
        //给form添加一个id，后面我们编辑的时候需要用到id这个字段
        this.form.id = id;
      });
    },
    //确定编辑
    edit() {
      this.form.menus = JSON.stringify(this.$refs.tree.getCheckedKeys());
      roleEdit(this.form).then((res) => {
        if (res.data.code == 200) {
          //成功的弹窗
          successAlert(res.data.msg);
          //关闭对话框并清空数据
          this.cancel();
          //重新请求列表数据
          this.reqRoleList();
        } else {
          //失败的弹窗
          warningAlert(res.data.msg);
        }
      });
    },
  },
  mounted() {
    //页面一进来调动一下menu的请求，menuList才有数据
    this.reqMenuList();
  },
};
</script>